package com.schoolvisitors.repository;

import com.schoolvisitors.entity.Instrument;
import com.schoolvisitors.entity.InstrumentSubType;
import jakarta.persistence.LockModeType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface InstrumentRepository extends JpaRepository<Instrument, Long> {

//    Instrument findById(Long instrumentId);
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("SELECT c FROM Instrument c WHERE c.id = :id and c.delete = false")
    Optional<Instrument> findByIdForLock(@Param("id") Long id);

    Optional<Instrument> findByIdAndDeleteFalse(Long idBy);

    List<Instrument> findAllByDeleteFalse();
    Page<Instrument> findAllByDeleteFalse(Pageable delete);

    boolean existsBySubType(InstrumentSubType subType);
}
